<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts'

  require('echarts/theme/macarons') // echarts theme
  import {debounce} from '@/utils'

  const animationDuration = 1000

  export default {
    name: 'category-chart',
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      },
      xData: Array,
      yData: Array,
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      yData() {
        this.renderChart();
      }
    },
    mounted() {
      this.initChart()
      this.__resizeHanlder = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHanlder)
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      window.removeEventListener('resize', this.__resizeHanlder)
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.renderChart()
      },
      renderChart(){
        const series = [
          {
            name: '',
            type: 'line',
            stack: 'vistors',
            barWidth: '60%',
            data: this.yData,
            animationDuration
          }
        ];

        this.chart.setOption({
          tooltip: {
            trigger: 'axis',
            // axisPointer: { // 坐标轴指示器，坐标轴触发有效
            //   type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            // }
            formatter(params,ticket,callback){
              console.log(params)
              var res = params[0].name + '<br/>';
              res += '充值: 500<br/>';
              res += '对比上月：+12.58%';
              // setTimeout(function (){
              //   // 仅为了模拟异步回调
              //   callback(ticket, res);
              // }, 1000)
              return res;
            },
          },
          // toolbox: {
          //   show: true,
          //   feature: {
          //     dataZoom: {show: true},
          //     dataView: {show: true},
          //     magicType: {show: true, type: ['line', 'bar']},
          //     restore: {show: true},
          //     saveAsImage: {show: true}
          //   }
          // },
          calculable: true,
          dataZoom: [{
            type: 'inside',
            show: true,
            realtime: true,
            start: 0,
            end: 100
          }, {
            type: 'slider'
          }],

          xAxis: [
            {
              type: 'category',
              boundaryGap: false,
              data: this.xData,
              // splitNumber: 1,
              // axisLabel: {
              //   interval: 1,
              //   formatter: function (val) {
              //     var str = val.substring(0, 10);
              //     return str;
              //   }
              // }
            }
          ],
          yAxis: [
            {
              type: 'value',
              // axisTick: {
              //   show: false
              // },
              // data: [],
              // axisLabel: {
              //   formatter: '{value} 人'
              // }
            }
          ],
          series,
        });
      }
    }
  }
</script>
